'use strict';
module.exports = {
	/**
	 * 特别重要！！！
	 * 特别重要！！！
	 * 特别重要！！！
	 * 这个页面是巨量系统第三方监测链接的数据接收函数，切勿删除！
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			project_id,
			promotion_id,
			uid,
			pt,
			timestamp,
			ip,
			ad_id,
			cid,
			advertiser_id,
			os,
			ua,
			callback,
			tfid,
			invite_code
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		var time = new Date().getTime()
		//记录广告平台发过来的触点信息，并保存到数据库，以备后面流程调用
		let addchudian = await db.collection('jl_chudian').add({
			uid: uid,
			pt: pt || "douyin", //平台（快手/抖音/其他等等）
			tfid: tfid || 0,
			invite_code: invite_code || 0,
			ts: timestamp, //客户端发生广告点击事件的时间，以毫秒为单位时间戳
			ip: ip, //用户客户端IP
			aid: ad_id, //广告计划id
			cid: cid, //广告创意 id，长整型
			ad_id: advertiser_id, //广告主id
			project_id: project_id, //项目ID
			promotion_id: promotion_id, //广告ID
			os: os, //操作系统平台
			ua: ua, //用户代理(User Agent)
			callback: callback, //一些跟广告信息相关的回调参数，内容是一个加密字符串，在调用事件回传接口的时候会用到
			time: time,
			time_str: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
		})

		//转发数据到新的 url
		// let url =
		// 	"https://log1.17173.com/hgtkio?pt=douyin&timestamp=" + timestamp + "&ip=" + ip + "&aid=" + aid +
		// 	"&cid=" + cid + "&advertiser_id=" + advertiser_id + "&os=" + os + "&ua=" + ua + "&callback=" +
		// 	callback;
		// console.log("url=", url);
		// let transferData = await uniCloud.httpclient.request(url, {
		// 	method: 'GET',
		// 	data: {}
		// })

		//巨量广告触点链接官方文档：https://open.oceanengine.com/labels/7/docs/1696710655781900

		// res.encode = encodeURIComponent("https://www.baidu1.com/");
		// res.originalParam = originalParam;
		// res.useragent = originalParam.context.CLIENTUA;
		// res.ip = originalParam.context.CLIENTIP;

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}